package com.zik.rader_and_video.controller;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.zik.rader_and_video.po.Errors;
import com.zik.rader_and_video.service.ConfigurationService;
import com.zik.rader_and_video.service.IErrorsService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author: zhouxy
 * @description:
 * @operation:
 **/
@Slf4j
@RestController
@RequestMapping("Wx")
public class WxController {
    @Autowired
    protected WxMpService wxMpService;

    @Autowired
    private ConfigurationService configurationService;

    @Autowired
    private IErrorsService errorsService;

    @GetMapping("/1")
    public void wxPush() {
        Errors errors = new Errors();
        errors.setErrorTag("fall");
        errors.setErrorData("跌倒事件");
        errors.setErrorValue(1);
        errorsService.save(errors);
        JSONObject accessToken = configurationService.getAccessToken();
        String token=accessToken.getStr("access_token");
        log.info("获取到accesstoken为 == 》"+token);
        //获取用户列表
        JSONObject userList = configurationService.getUserList(token);

        log.info("{}",userList);
        JSONArray openids = userList.getJSONObject("data").getJSONArray("openid");
        for (Object openid:openids) {
            configurationService.sendMsg(token,openid.toString());
        }
    }
}
